﻿@model CategoryNavigationModel
@using Nop.Web.Models.Catalog;

@functions{
    public bool BreadCrumbContainsCurrentCategoryId(CategorySimpleModel category)
    {
        if (Model.CurrentCategoryId == 0)
            return false;

        if (category.Id == Model.CurrentCategoryId)
            return true;

        foreach (var subCategory in category.SubCategories)
        {
            if (BreadCrumbContainsCurrentCategoryId(subCategory))
            {
                return true;
            }
        }

        return false;
    }
}
@helper RenderCategoryLine(CategorySimpleModel category)
{
    <li class="@(category.Id == Model.CurrentCategoryId ? "active" : "") @(category.SubCategories.Count > 0 ? " dropdown " : "") @(category.SubCategories.Count > 0 && category.Id == Model.CurrentCategoryId ? " open " : "")">
        @if (category.SubCategories.Count > 0)
        {
            <a href="@Url.RouteUrl("Category", new { SeName = category.SeName })" style="width:50%" class="pull-left">
                <span>@category.Name</span>
                @if (category.NumberOfProducts.HasValue)
                {
                    <text> </text>@T("Categories.TotalProducts", category.NumberOfProducts.Value)
                }
            </a>

            <a href="#" data-toggle="dropdown" style="width:50%" class="dropdown-toggle pull-right text-right"><span class="glyphicon glyphicon-circle-arrow-down"></span></a>
            <div style="clear:both;"></div>
        }
        else
        {
            <a href="@Url.RouteUrl("Category", new { SeName = category.SeName })">
                <span>@category.Name</span>
                @if (category.NumberOfProducts.HasValue)
                {
                    <text> </text>@T("Categories.TotalProducts", category.NumberOfProducts.Value)
                }
            </a>
        }

        @{
            //if (category.Id == Model.CurrentCategoryId ||
            //    category.SubCategories.Count(BreadCrumbContainsCurrentCategoryId) > 0)
            //{
            if (category.SubCategories.Count > 0)
            {
                <ul class="dropdown-menu category-navigasion-subcategory">
                    @foreach (var subCategory in category.SubCategories)
                    {
                        @RenderCategoryLine(subCategory)
                    }
                </ul>
            }
    //}
        }
    </li>
}
@if (Model.Categories.Count > 0)
{
    <nav class="hz-navbar hz-navbar-default panel panel-default form-panel" role="navigation">
        <div class="hz-navbar-header">
            <button type="button" class="hz-navbar-toggle" data-toggle="collapse" data-target="#categories-sidebar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="hz-navbar-brand visible-xs" href="#">@T("Categories")</a>
        </div>
        <div class="panel-heading hidden-xs">
            <h3>@T("Categories")</h3>
        </div>
        <div class="hz-navbar-collapse collapse" id="categories-sidebar">
            <ul class="nav hz-navbar-nav">
                @foreach (var category in Model.Categories)
                {
                    @RenderCategoryLine(category)
                }
            </ul>

        </div><!-- /.navbar-collapse -->
    </nav>


}